/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.mailfolder;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.mailfolder.Mailbox;
import com.unis.service.mailfolder.MailboxService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-02-21
 */
@Controller
@RequestMapping("/mailbox")
public class MailboxController {

    private MailboxService mailboxService;

    /****************************************
     * Insert a instace of Mailbox
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertMailbox(@RequestBody Mailbox mailbox) throws Exception {
        mailbox.setStatmentId("Mailbox.insert");
        String status = "failure";
        mailboxService.insert(mailbox);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Mailbox
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Mailbox mailbox) throws Exception {
        mailbox.setStatmentId("Mailbox.update");
        String status = "failure";
        mailboxService.update(mailbox);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Mailbox
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Mailbox mailbox) throws Exception {
        mailbox.setStatmentId("Mailbox.delete");
        String status = "failure";
        mailboxService.batchDelete(mailbox);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Mailbox
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Mailbox mailbox = new Mailbox();
        mailbox.setStatmentId("Mailbox.queryAsList");
        BeanUtils.populate(mailbox, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(mailbox, pageMap);
        List<BaseModel> mailboxList = mailboxService.queryAsList(mailbox);
        Map<String, Object> renderMap = GridUIMapper.render(mailbox, mailboxList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Mailbox
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Mailbox mailbox) throws Exception {
        mailbox.setStatmentId("Mailbox.queryAsObject");
        return mailboxService.queryAsObject(mailbox);
    }

    @RequestMapping("/page")
    public String forword() {
        return "mailfolder/mailboxList";
    }

    @Resource
    public void setMailboxService(MailboxService mailboxService) {
        this.mailboxService = mailboxService;
    }
}
